package com.engine.hitech.duss.budget.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

/**
 * FileName: Test.java
 * 类的详细说明
 *
 * @author 插入数据工具类
 * @version 1.00版本
 * @Date 2022-08-17 11:52
 */
public class DBUtil {


    public static boolean Update_edc_tasknode(String taskid, String pageDateId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select max(id) from edc_tasknode where taskid=?", taskid );
        rs.next();
        String id = Util.null2String( rs.getString( 1 ) );
        return rs.executeUpdate( "update edc_tasknode set status=3 ,formData=" + pageDateId + " where id=" + id );
    }

    /**
     * 插入明细数据edc_pagedatas；
     */
    public static boolean Insert_edc_pagedatas( String appid,String dataid,String formid,String pageDataId) {
        RecordSet rs = new RecordSet();
        Map<String, String> map = new HashMap<>(  );
        String uuid = UUID.randomUUID().toString().replaceAll( "-", "" );
        rs.executeQuery( "select id,name from edc_page where appid=?", appid );
        rs.next();
        map.put( "uuid", uuid );
        map.put( "name", Util.null2String(rs.getString( "name" )) );
        map.put( "pageid", Util.null2String(rs.getString( "id" )) );
        map.put( "dataid", dataid);
        map.put( "formid", formid);
        map.put( "pageDataId", pageDataId);
        String sql = InsertSql( map, "edc_pagedatas" );
        return rs.execute( sql );
    }

    /**
     * 插入 Insert_edc_pagedata数据主表数据  返回id
     *
     * @return id
     */
    public static String Insert_edc_pagedata() {
        RecordSet rs = new RecordSet();
        String uuid = UUID.randomUUID().toString().replaceAll( "-", "" );
        boolean flag = rs.executeUpdate( "insert into edc_pagedata(uuid) values('" + uuid + "')" );
        String id = "";
        if (flag) {
            rs.executeQuery( "select id from edc_pagedata where uuid='" + uuid + "'" );
            rs.next();
            id = Util.null2String( rs.getString( "id" ) );
        }
        return id;
    }

    /**
     * 插入主表信息 返回 id
     *
     * @param tableName 插入的表单名称
     * @param map       含有taskid的表单字段数据
     * @return
     */
    public static String InsertMainTable(String tableName, Map<String, String> map) {
        RecordSet rs = new RecordSet();
        String id = "";
        String datetime = DateUtil.getFullDate();
        String date = datetime.substring( 0, 10 );
        String time = datetime.substring( 11 );
        String uuid = UUID.randomUUID().toString().replaceAll( "-", "" );
        map.put( "ip", "172.27.199.91" );
        map.put( "uuid", uuid );
        map.put( "modedatacreater", "1" );
        map.put( "modedatacreatedate", date );
        map.put( "modedatacreatetime", time );
        map.put( "modedatamodifier", "1" );
        map.put( "modedatamodifydatetime", date + " " + time );
        String sql = InsertSql( map, tableName );
        boolean flag = rs.executeUpdate( sql );
        if (flag) {
            rs.executeQuery( "select id from " + tableName + " where uuid=?", uuid );
            rs.next();
            id = Util.null2String( rs.getString( "id" ) );
        }
        return id;
    }

    /**
     * 拼接插入语句
     *
     * @param zd
     * @param tablename 表名
     * @return
     */
    public static String InsertSql(Map<String, String> zd, String tablename) {
        StringBuffer sql = new StringBuffer( "insert into " + tablename + "(" );
        Set<String> set = zd.keySet();
        for (String string : set) {
            sql.append( string + "," );
        }
        sql = sql.deleteCharAt( sql.length() - 1 );
        sql.append( ") values(" );
        for (String string : set) {
            sql.append( "N'" + zd.get( string ).replace( "'", " " ) + "'," );
        }
        sql = sql.deleteCharAt( sql.length() - 1 );
        sql.append( ")" );
        return sql.toString();
    }

    //通用的插入操作
    public static boolean insertData(String tablename, Map<String, Object> map) {

        String insertsql = "insert into " + tablename + "";
        String keyString = "";
        String valueString = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (map.get( key ) == null || "".equals( map.get( key ).toString() )) {
                continue;
            }
            keyString += key + ",";
            valueString += "'" + map.get( key ).toString() + "',";
        }
        if (keyString.endsWith( "," )) {
            keyString = keyString.substring( 0, keyString.length() - 1 );
        }
        if (valueString.endsWith( "," )) {
            valueString = valueString.substring( 0, valueString.length() - 1 );
        }
        insertsql += "(" + keyString + ") values(" + valueString + ")";
        RecordSet rs = new RecordSet();
        boolean flag = rs.executeUpdate( insertsql );
        return flag;
    }
}
